package com.projectx.action.contact;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.util.ActivityManage;

import java.util.ArrayList;
import java.util.List;

public class DeleteContact extends AppCompatActivity {
    private ImageView back,delete,delete_person;
    private EditText editText;
    private Button search;
    private RecyclerView recyclerView;
    private List<String> data=new ArrayList<>();
    private List<Bitmap> bitmaps=new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_contact);
        ActivityManage.addActivity("DeleteContact",this);

        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }
        delete_person=findViewById(R.id.delete_person);
        back=findViewById(R.id.back);
        delete=findViewById(R.id.delete);
        editText=findViewById(R.id.edittext);
        search=findViewById(R.id.search);
        recyclerView=findViewById(R.id.recycler_view);

        data.add("测试");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final  DeleteContectAdapter deleteContectAdapter=new DeleteContectAdapter(this,TabFragment.data_person,bitmaps);
        recyclerView.setAdapter(deleteContectAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManage.exitActivity("DeleteContact");
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteContectAdapter.notifyDataSetChanged();
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });
        delete_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i=0;i<TabFragment.data_person.size();i++){
                    if (DeleteContectAdapter.count[i]%2!=0){
                        Log.d("lzw",TabFragment.data_person.get(i)+"删错了吧");
                        TabFragment.data_person.remove(TabFragment.data_person.get(i));
                        //删除好友
                    }
                }
                ActivityManage.exitActivity("DeleteContact");
            }
        });
        editText.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }//文本改变之前执行

            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                deleteContectAdapter.notifyDataSetChanged();
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    delete.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    delete.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);
                }
            }

            public void afterTextChanged(Editable s) {
            }//文本改变之后执行
        });
    }
}

 class DeleteContectAdapter extends RecyclerView.Adapter<DeleteContectAdapter.VH> {

    private Context context;
    private List<String> data;
    private  List<Bitmap>bitmaps;
    protected static int[] count=new int[100000];
    public DeleteContectAdapter(Context context, List<String> data, List<Bitmap>bitmaps) {
        this.context = context;
        this.data = data;
        this.bitmaps=bitmaps;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_delete_item, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, final int position) {
        holder.textView.setText(data.get(position));
        try {
            holder.imageView.setImageBitmap(bitmaps.get(position));
            if(bitmaps.get(position).equals(null)){
                holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
            }
        }catch (NullPointerException e){
            holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
        }catch (IndexOutOfBoundsException e){
            Log.e("lzw","头像list越界了");
        }
        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                count[position]++;
                if (count[position]%2!=0){
                    holder.check.setVisibility(View.VISIBLE);
                }
                else{
                    holder.check.setVisibility(View.GONE);
                }
                // 点击事件
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        TextView textView;
        ImageView imageView,check;
        View click;
        public VH(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            imageView= itemView.findViewById(R.id.imageView);
            click=itemView.findViewById(R.id.click);
            check=itemView.findViewById(R.id.check);
        }
    }
}

